<?php

namespace App\Http\Controllers;

use App\Helpers\ResponseHelper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Overtrue\LaravelWeChat\Facade as EasyWeChat;

class UploadController extends Controller
{
    //
    public function upload(Request $request)
    {
        switch ($request->input('type')) {
            case 1:
                $path = 'images/avatar';
                break;
            case 2:
                $path = 'images/id_card';
                break;
            case 3:
                $path = 'images/order';
                break;
            case 4:
                $path = 'images/feedback';
                break;
            default:
                $path = 'images';
                break;
        }
        // 验证上传请求中的文件
        $validator = Validator::make($request->all(), [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif',
        ]);

        if ($validator->fails()) {
            return ResponseHelper::error(message: $validator->errors());
        }

        // 获取上传的文件
        $file = $request->file('image');

        // 生成唯一文件名并存储文件到 public 目录
        $filePath = $file->store($path, 'oss');

        $completePath = Storage::disk('oss')->url($filePath);

        return ResponseHelper::success(data: [
            'completePath' => $completePath,
            'path' => $filePath
        ]);
        /*// 调用图片安全内容检测接口
        $result = EasyWeChat::miniProgram()->content_security->checkImage($completePath);

        if ($result['errcode'] == 0) {
            // 返回上传成功的响应，包含图片的 URL
            return ResponseHelper::success(data: [
                'completePath' => $completePath,
                'path' => $filePath
            ]);
        } else {
            return ResponseHelper::error(message: $result['errmsg']);
        }*/
    }
}
